/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;

import java.util.logging.Logger;

import l1j.server.Config;
import l1j.server.server.ClientThread;
import l1j.server.server.GMCommands;
import l1j.server.server.Opcodes;
import l1j.server.server.datatables.ChatLogTable;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1MonsterInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.skill.L1SkillId;
import l1j.server.server.serverpackets.S_ChatPacket;
import l1j.server.server.serverpackets.S_NpcChatPacket;
import l1j.server.server.serverpackets.S_OwnCharStatus;
import l1j.server.server.serverpackets.S_ServerMessage;

// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket

// chat opecode type
// 通常 0x44 0x00
// 叫び(!) 0x44 0x00
// ささやき(") 0x56 charname
// 全体(&) 0x72 0x03
// トレード($) 0x44 0x00
// PT(#) 0x44 0x0b
// 血盟(@) 0x44 0x04
// 連合(%) 0x44 0x0d
// CPT(*) 0x44 0x0e

public class C_RequestChat extends ClientBasePacket {

	private static final String _C__0B_REQCHAT = "[C] C_RequestChat";
	private static Logger _log = Logger
			.getLogger(C_RequestChat.class.getName());

	public C_RequestChat(byte abyte0[], ClientThread clientthread) {
		super(abyte0);

		L1PcInstance pc = clientthread.getActiveChar();
		int chatType = readC();
		String chatText = readS();
		if (pc.hasSkillEffect(L1SkillId.SILENCE)
				|| pc.hasSkillEffect(L1SkillId.AREA_OF_SILENCE)
				|| pc.hasSkillEffect(L1SkillId.STATUS_POISON_SILENCE)) {
			return;
		}
		if (pc.hasSkillEffect(1005)) { // チャット禁止中
			pc.sendPackets(new S_ServerMessage(242)); // 現在チャット禁止中です。
			return;
		}

		if (chatType == 0) { // 通常チャット
			if (pc.isGhost() && !(pc.isGm() || pc.isMonitor())) {
				return;
			}
			// GMコマンド
			if (chatText.startsWith(".")) {
				String cmd = chatText.substring(1);
				GMCommands.getInstance().handleCommands(pc, cmd);
				return;
			}

			ChatLogTable.getInstance().storeChat(pc, null, chatText,chatType);
			S_ChatPacket s_chatpacket = new S_ChatPacket(pc, chatText,
					Opcodes.S_OPCODE_NORMALCHAT, 0);
			if (!pc.isExclude(pc.getName())) {
				pc.sendPackets(s_chatpacket);
			}
			for (L1PcInstance listner : L1World
					.getInstance().getRecognizePlayer(pc)) {
				if (!listner.isExclude(pc.getName())) {
					listner.sendPackets(s_chatpacket);
				}
			}
			//ドッペル処理
			for (L1Object obj : L1World.getInstance().getVisibleObjects(pc)) {
				if (obj instanceof L1MonsterInstance) {
					L1MonsterInstance mob = (L1MonsterInstance) obj;
					if (mob.getNpcTemplate().is_doppel()
							&& mob.getName().equals(pc.getName())) {
						mob.broadcastPacket(new S_NpcChatPacket(mob,
								chatText, 0));
					}
				}
			}
		} else if (chatType == 2) { // 叫び
			if (pc.isGhost()) {
				return;
			}
			ChatLogTable.getInstance().storeChat(pc, null, chatText, chatType);
			S_ChatPacket s_chatpacket = new S_ChatPacket(pc, chatText,
					Opcodes.S_OPCODE_NORMALCHAT, 2);
			if (!pc.isExclude(pc.getName())) {
				pc.sendPackets(s_chatpacket);
			}
			for (L1PcInstance listner : L1World.getInstance().getVisiblePlayer(
					pc, 50)) {
				if (!listner.isExclude(pc.getName())) {
					listner.sendPackets(s_chatpacket);
				}
			}

			//ドッペル処理
			for (L1Object obj : L1World.getInstance().getVisibleObjects(pc)) {
				if (obj instanceof L1MonsterInstance) {
					L1MonsterInstance mob = (L1MonsterInstance) obj;
					if (mob.getNpcTemplate().is_doppel()
							&& mob.getName().equals(pc.getName())) {
						for (L1PcInstance listner : L1World.getInstance()
								.getVisiblePlayer(mob, 50)) {
							listner.sendPackets(
									new S_NpcChatPacket(mob, chatText, 2));
						}
					}
				}
			}
		} else if (chatType == 3) { // 全体チャット
			if (pc.isGm()) {
				ChatLogTable.getInstance().storeChat(pc, null, chatText,
						chatType);
				L1World.getInstance().broadcastPacketToAll(
						new S_ChatPacket(pc, chatText,
								Opcodes.S_OPCODE_GLOBALCHAT, 3));
			} else if (pc.getLevel() >= Config.GLOBAL_CHAT_LEVEL) {
				if (L1World.getInstance().isWorldChatElabled()) {
					if (pc.get_food() >= 2) {
						pc.set_food(pc.get_food() - 2);
						ChatLogTable.getInstance().storeChat(pc, null,
								chatText, chatType);
						pc.sendPackets(new S_OwnCharStatus(pc));
						for (L1PcInstance listner : L1World
								.getInstance().getAllPlayers()) {
							if (listner.isExclude(pc.getName())) {
								listner.sendPackets(
										new S_ChatPacket(pc, chatText,
										Opcodes.S_OPCODE_GLOBALCHAT, 3));
							}
						}
					} else {
						pc.sendPackets(new S_ServerMessage(462)); // \f1空腹のためチャットできません。
					}
				} else {
					pc.sendPackets(new S_ServerMessage(510)); // 現在ワールドチャットは停止中となっております。しばらくの間ご了承くださいませ。
				}
			} else {
				pc.sendPackets(new S_ServerMessage( // レベル%0未満のキャラクターはチャットができません。
						195, String.valueOf(Config.GLOBAL_CHAT_LEVEL)));
			}
		} else if (chatType == 4) { // 血盟チャット
			if (pc.getClanid() != 0) { // クラン所属中
				L1Clan clan = L1World.getInstance().getClan(pc.getClanname());
				if (clan != null) {
					ChatLogTable.getInstance().storeChat(pc, null, chatText,
							chatType);
					S_ChatPacket s_chatpacket = new S_ChatPacket(pc, chatText,
							Opcodes.S_OPCODE_GLOBALCHAT, 4);
					L1PcInstance[] clanMembers = clan.getOnlineClanMember();
					for (L1PcInstance listner : clanMembers) {
						if (!listner.isExclude(pc.getName())) {
							listner.sendPackets(s_chatpacket);
						}
					}
				}
			}
		} else if (chatType == 11) { // パーティーチャット
			if (pc.isInParty()) { // パーティー中
				ChatLogTable.getInstance().storeChat(pc, null, chatText,
						chatType);
				S_ChatPacket s_chatpacket = new S_ChatPacket(pc, chatText,
						Opcodes.S_OPCODE_GLOBALCHAT, 11);
				L1PcInstance[] partyMembers = pc.getParty().getMembers();
				for (L1PcInstance listner : partyMembers) {
					if (!listner.isExclude(pc.getName())) {
						listner.sendPackets(s_chatpacket);
					}
				}
			}
		} else if (chatType == 13) { // 連合チャット
			if (pc.getClanid() != 0) { // クラン所属中
				L1Clan clan = L1World.getInstance().getClan(pc.getClanname());
				if (clan != null) {
					ChatLogTable.getInstance().storeChat(pc, null, chatText,
							chatType);
					S_ChatPacket s_chatpacket = new S_ChatPacket(pc, chatText,
							Opcodes.S_OPCODE_GLOBALCHAT, 13);
					L1PcInstance[] clanMembers = clan.getOnlineClanMember();
					for (L1PcInstance listner : clanMembers) {
						if (!listner.isExclude(pc.getName())) {
							listner.sendPackets(s_chatpacket);
						}
					}
				}
			}
		} else if (chatType == 14) { // チャットパーティー
			byte opcode = Opcodes.S_OPCODE_GLOBALCHAT;
			byte byte1 = 14;
		}
	}

	@Override
	public String getType() {
		return _C__0B_REQCHAT;
	}
}
